# -*- coding:utf-8 -*-

from fastapi import APIRouter, Depends, Form
from sqlalchemy.ext.asyncio import AsyncSession
from starlette.requests import Request
from typing import List
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.entity.vo.import_vo import ImportModel
from module_admin.service.import_service import ImportService
from module_admin.service.login_service import LoginService
from module_admin.aspect.data_scope import GetDataScope
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.annotation.log_annotation import Log
from utils.response_util import ResponseUtil
from utils.common_util import bytes2file_response

from module_doc.entity.vo.bg_file_category_vo import BgFileCategoryPageModel, BgFileCategoryModel, \
    BgFileCategoryAddModel
from module_doc.service.bg_file_category_service import BgFileCategoryService

bgFileCategoryController = APIRouter(prefix='/file/category', dependencies=[Depends(LoginService.get_current_user)])




@bgFileCategoryController.get('/list', dependencies=[Depends(CheckUserInterfaceAuth('file:category:query'))])
async def get_bg_file_category_list(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: BgFileCategoryPageModel = Depends( BgFileCategoryPageModel.as_query),
        data_scope_sql: str = Depends(GetDataScope('BgFileCategory'))
):
    bg_file_category_result = await BgFileCategoryService.get_bg_file_category_list(query_db, page_query, data_scope_sql)

    return ResponseUtil.success(model_content=bg_file_category_result)

@bgFileCategoryController.get('/tree', dependencies=[Depends(CheckUserInterfaceAuth('file:category:query'))])
async def get_bg_file_category_tree(
        request: Request,
        query_db: AsyncSession = Depends(get_db),
        page_query: BgFileCategoryPageModel = Depends(BgFileCategoryPageModel.as_query)
):
    """
    获取文件分类树形结构
    """
    tree_data = await BgFileCategoryService.get_bg_file_category_tree(query_db, page_query)
    return ResponseUtil.success(data=tree_data)

@bgFileCategoryController.get('/children/{parentId}', dependencies=[Depends(CheckUserInterfaceAuth('file:category:query'))])
async def get_bg_file_category_children(
        request: Request,
        parentId: int,
        query_db: AsyncSession = Depends(get_db)
):
    """
    获取指定父分类下的子分类列表
    """
    children_data = await BgFileCategoryService.get_bg_file_category_children(query_db, parentId)
    return ResponseUtil.success(data=children_data)


@bgFileCategoryController.get('/getById/{bgFileCategoryId}', dependencies=[Depends(CheckUserInterfaceAuth('file:category:query'))])
async def get_bg_file_category_by_id(
        request: Request,
        bgFileCategoryId: int,
        query_db: AsyncSession = Depends(get_db),
        data_scope_sql: str = Depends(GetDataScope('BgFileCategory'))
):
    bg_file_category = await BgFileCategoryService.get_bg_file_category_by_id(query_db, bgFileCategoryId)
    return ResponseUtil.success(data=bg_file_category)


@bgFileCategoryController.post('/add', dependencies=[Depends(CheckUserInterfaceAuth('file:category:add'))])
@Log(title='bg_file_category', business_type=BusinessType.INSERT)
async def add_bg_file_category (
    request: Request,
    add_model: BgFileCategoryAddModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):


    add_model.create_by = current_user.user.user_id
    add_model.dept_id = current_user.user.dept_id
    add_dict_type_result = await BgFileCategoryService.add_bg_file_category(query_db, add_model)
    return ResponseUtil.success(data=add_dict_type_result)

@bgFileCategoryController.put('/update', dependencies=[Depends(CheckUserInterfaceAuth('file:category:edit'))])
@Log(title='bg_file_category', business_type=BusinessType.UPDATE)
async def update_bg_file_category(
    request: Request,
    edit_model: BgFileCategoryModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_dict_type_result = await BgFileCategoryService.update_bg_file_category(query_db, edit_model)
    return ResponseUtil.success(data=add_dict_type_result)


@bgFileCategoryController.delete('/delete/{bgFileCategoryIds}', dependencies=[Depends(CheckUserInterfaceAuth('file:category:remove'))])
@Log(title='bg_file_category', business_type=BusinessType.DELETE)
async def del_bg_file_category(
    request: Request,
    bgFileCategoryIds: str,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    ids = bgFileCategoryIds.split(',')
    del_result = await BgFileCategoryService.del_bg_file_category(query_db, ids)
    return ResponseUtil.success(data=del_result)

@bgFileCategoryController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('file:category:export'))])
@Log(title='bg_file_category', business_type=BusinessType.EXPORT)
async def export_bg_file_category(
    request: Request,
    bg_file_category_form: BgFileCategoryPageModel = Form(),
    query_db: AsyncSession = Depends(get_db),
    data_scope_sql: str = Depends(GetDataScope('BgFileCategory')),
):
    # 获取全量数据
    export_result = await BgFileCategoryService.export_bg_file_category_list(
        query_db, bg_file_category_form, data_scope_sql
    )
    return ResponseUtil.streaming(data=bytes2file_response(export_result))

@bgFileCategoryController.post('/import', dependencies=[Depends(CheckUserInterfaceAuth('file:category:import'))])
async def import_bg_file_category(request: Request,
                      import_model: ImportModel,
                      query_db: AsyncSession = Depends(get_db),
                      current_user: CurrentUserModel = Depends(LoginService.get_current_user)
    ):
    """
    导入数据
    """
    await ImportService.import_data(query_db, import_model, current_user)
    return ResponseUtil.success()
